﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Lib.Bases;
using Lib.Utils;

namespace MS.INV.Inventory
{
    public partial class KiemKeKho : Lib.Bases.Forms.BaseForm
    {
        SysObj.Kho_KiemKe _cls = new SysObj.Kho_KiemKe();

        public KiemKeKho()
        {
            InitializeComponent();
        }

        protected override void OnHandleDestroyed(EventArgs e)
        {
            _cls.Dispose();
            base.OnHandleDestroyed(e);
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            try
            {
                LoadHistory();
            }
            catch (Exception ex)
            {
                Lib.Bases.SysError.RaiseError(ex, this.Name, "Form Load");
            }
        }

        private void LoadHistory()
        {
            cbHistory.SetDataSource(_cls.GetHistory(), "DienGiai", "IdKiemKe", "DienGiai#Lịch sử");
            cbHistory.EditValue = null;
        }

        private void cbHistory_EditValueChanged(object sender, EventArgs e)
        {
            if (Cvert.ToInt32(cbHistory.EditValue, 0) <= 0) return;
            try
            {
                dtNgay.Enabled = false;
                _cls.IdKiemKe = Cvert.ToInt32(cbHistory.EditValue, 0);
                _cls.ReadData();
                dtNgay.EditValue = _cls.Ngay;
                txtDescr.Text = _cls.DienGiai;
                _grid.DataSource = _cls.Get_ChiTiet();

                btEdit.Enabled = true;
                btUpdate.Enabled = false;
            }
            catch (Exception ex)
            {
                _cls.ResetData();
                Lib.Bases.SysError.RaiseError(ex, this.Name, "History_EditValueChanged");
            }
        }

        private void btNew_Click(object sender, EventArgs e)
        {
            dtNgay.EditValue = DateTime.Now;
            dtNgay.Enabled = true;
            dtNgay.Focus();
            txtDescr.Text = "";
            btUpdate.Enabled = true;
            btEdit.Enabled = false;
            cbHistory.EditValue = null;
        }

        private void dtNgay_Validated(object sender, EventArgs e)
        {
            try
            {
                using (SysObj.Inf_InventoryStock inf = new SysObj.Inf_InventoryStock("KiemKe",
                                                    Convert.ToDateTime(dtNgay.EditValue), Convert.ToDateTime(dtNgay.EditValue)))
                {
                    _grid.DataSource = inf.Excutive();
                }
            }
            catch (Exception ex)
            {
                Lib.Bases.SysError.RaiseError(ex, this.Name, "dtNgay_Validated");
            }
        }

        private void btUpdate_Click(object sender, EventArgs e)
        {
            try
            {

                DataTable DT = _grid.GetAcceptChanges();
                
                if (DT == null || DT.Rows.Count <= 0)
                {
                    Msg.ErrOK("Không tìm thấy dữ liệu cần cập nhật.");
                    return;
                }

                _cls.Ngay = Convert.ToDateTime(dtNgay.EditValue);
                _cls.Detail = DT;
                _cls.DienGiai = txtDescr.Text;
                _cls.OnUpdate();

                LoadHistory();

                Msg.InforOK("Cập nhật thành công.");
            }
            catch (Exception ex)
            {
                Lib.Bases.SysError.RaiseError(ex, this.Name, "OnUpdate");
            }
        }

        private void btEdit_Click(object sender, EventArgs e)
        {
            btUpdate.Enabled = true;
            btEdit.Enabled = false;
        }

        private void btEx_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
